//Source file: D:\\WTK104\\apps\\Toddtris\\src\\com\\toddsoft\\toddtris\\model\\Block.java

package com.toddsoft.toddtris.model;


public class Block {
    private int blockNum;
    public static final int NUM_BLOCKS = 7;
    private static final int [][][] baseElems = 
            {
                {
                    {1, 1},
                    {1, 1}
                },
                {
                    {0, 2, 0},
                    {0, 2, 2},
                    {0, 2, 0}
                },
                {
                    {0, 3, 3},
                    {0, 3, 0},
                    {0, 3, 0}
                },
                {
                    {0, 4, 0},
                    {0, 4, 0},
                    {0, 4, 4}
                },
                {
                    {0, 5, 0, 0},
                    {0, 5, 0, 0},
                    {0, 5, 0, 0},
                    {0, 5, 0, 0}
                },
                {
                    {0, 0, 6},
                    {0, 6, 6},
                    {0, 6, 0}
                },
                {
                    {0, 7, 0},
                    {0, 7, 7},
                    {0, 0, 7}
                }
            };
    
    /**
     * first index is block number, second is rotation index,
     * third and forth are the shape of the block
     */
    private static final int allElems[][][][] 
            = new int [baseElems.length][4][][];

    private int rotationIndex;
    
    
    static {
        //initialise allBlocks
        for (int i = 0; i < baseElems.length; i++) {
            int [][] currentElems = baseElems[i];
            for (int j = 0; j < 4; j++) {
                allElems[i][j] = currentElems;
                if (j < 3) {
                    currentElems = rotateMatrix(currentElems);
                }
            }
        }     
    }
        
    /**
     * @param b
     */
    Block(int b) {
        this(b, 0);     
    }
    
    /**
     * @param blockNum
     * @param rotationIndex
     */
    Block(int blockNum, int rotationIndex) {
        this.blockNum = blockNum;
        this.rotationIndex = rotationIndex;     
    }
    
    /**
     * @param m
     * @return int[][]
     */
    private static int[][] rotateMatrix(int[][] m) {
        int [][] rotated = new int[m.length][m.length];
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m.length; j++) {
                rotated[m.length - 1 - j][i] = m[i][j];
            }
        }
        return rotated;     
    }
    
    /**
     * @return int
     */
    public int getSize() {
        return allElems[blockNum][rotationIndex].length;     
    }
    
    /**
     * @param x
     * @param y
     * @return int
     */
    public int getElem(int x, int y) {
        return allElems[blockNum][rotationIndex][x][y];     
    }
    
    void rotate() {
        rotationIndex = (++rotationIndex) % 4;     
    }
    
    void antirotate() {
        rotationIndex = (rotationIndex + 3) % 4;     
    }
    
    void reset() {
        rotationIndex = 0;     
    }
    
    /**
     * @return int
     */
    int getBlockNum() {
        return blockNum;     
    }
    
    /**
     * @return int
     */
    int getRotationIndex() {
        return rotationIndex;     
    }
}
